package gang.org.springframework.framework.metadata;

import java.lang.annotation.Annotation;
import java.util.Map;

public interface GangAnnotatedTypeMetadata {

    GangMergedAnnotations getAnnotations();

    default Map<String, Object> getAnnotationAttribute(String annotationName){
        return getAnnotationAttribute(annotationName, false);
    }

    /**
     * @param annotationName String value -> "gang.org.springframework.boot.autoconfigure.GImport"
     *                                    -> "gang.org.springframework.framework.annotation.GComponentScan"
     * @param classValuesAsString boolean value -> true submit
     * */
    default Map<String,Object> getAnnotationAttribute(String annotationName, boolean classValuesAsString)
    {
        GangMergedAnnotations annotations = getAnnotations();

        if (annotations == null) {
            return null;
        }

        /**
         * 业务意义
         * SpringBoot 加载 class类两种对应数据对象
         * 1）springboot 资源文件配置类 ServletWebServerFactoryAutoConfiguration-> SourceClass -> SimpleAnnotationMetadata -> MergedAnnotationsCollection
         * 2）springboot 核心文件启动类 ConfigurationClassPostProcessor -> SourceClass -> StandardAnnotationMetadata -> TypeMappedAnnotations
         * */
        GangMergedAnnotation<Annotation> annotation = annotations.get(annotationName, null, GangMergedAnnotationSelectors.firstDirectlyDeclared());

        // TODO 20211003
        if (annotation == null){
            return null;
        }

        GangAnnotationAttributes gangAnnotationAttributes = annotation.asAnnotationAttributes(GangMergedAnnotation.GangAdapt.values(classValuesAsString, true));
        return gangAnnotationAttributes;
    }

    default boolean isAnnotated(String annotationName){
        if (getAnnotations() == null) {
            return false;
        }
        return getAnnotations().isPresent(annotationName);
    }


}
